package com.cyj.dream.admin.persistence.manager.systems;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyj.dream.admin.model.request.SysOperationLogQueryRequest;
import com.cyj.dream.admin.model.system.SysOperationLog;
import com.cyj.dream.admin.persistence.service.ITbSysOperationLogService;
import com.cyj.dream.core.util.IsEmptyUtils;
import com.cyj.dream.core.util.date.DateReport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Description: 系统操作日志 管理类
 * @BelongsProject: dream-DreamChardonnay
 * @BelongsPackage: com.dream.DreamChardonnay.persistence.manager.systems
 * @Author: ChenYongJia
 * @CreateTime: 2021-04-16 10:31
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Slf4j
@Component
public class SysOperationLogManager {

    @Autowired
    private ITbSysOperationLogService iTbSysOperationLogService;

    /**
     * 分页查询操作日志信息
     *
     * @param page         分页参数
     * @param queryRequest 请求参数
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.dream.DreamChardonnay.model.system.SysOperationLog>
     * @date 2021/6/11 10:29
     * @author ChenYongJia
     * @version 1.0
     */
    public Page<SysOperationLog> list(Page<SysOperationLog> page, SysOperationLogQueryRequest queryRequest) {
        log.info("进入 SysOperationLongManager 分页查询操作日志信息，参数 page：{}，参数 param：{}", JSONObject.toJSONString(page), queryRequest);
        LambdaQueryWrapper<SysOperationLog> wrapper = new LambdaQueryWrapper<>();
        if (!IsEmptyUtils.isEmpty(queryRequest)) {
            // 请求记录id
            if (!IsEmptyUtils.isEmpty(queryRequest.getDreamId())) {
                wrapper.eq(SysOperationLog::getDreamId, queryRequest.getDreamId());
            }
            // 请求用户
            if (!IsEmptyUtils.isEmpty(queryRequest.getLogOperatorUserId())) {
                wrapper.eq(SysOperationLog::getLogOperatorUserId, queryRequest.getLogOperatorUserId());
            }
            // 请求IP
            if (!IsEmptyUtils.isEmpty(queryRequest.getLogOperatorIp())) {
                wrapper.like(SysOperationLog::getLogOperatorIp, queryRequest.getLogOperatorIp());
            }
            // 请求路径
            if (!IsEmptyUtils.isEmpty(queryRequest.getLogOperatorPath())) {
                wrapper.like(SysOperationLog::getLogOperatorPath, queryRequest.getLogOperatorPath());
            }
            // 请求类型
            if (!IsEmptyUtils.isEmpty(queryRequest.getLogOperatorRequestType())) {
                wrapper.eq(SysOperationLog::getLogOperatorRequestType, queryRequest.getLogOperatorRequestType());
            }
            // 请求结果
            if (!IsEmptyUtils.isEmpty(queryRequest.getLogOperatorResults())) {
                wrapper.like(SysOperationLog::getLogOperatorResults, queryRequest.getLogOperatorResults());
            }
        }

        wrapper.orderByDesc(true, SysOperationLog::getCreateTime);

        return iTbSysOperationLogService.page(page, wrapper);
    }

    /**
     * 添加系统操作日志对象
     *
     * @param sysOperationLog 系统操作日志对象
     * @return boolean
     * @date 2021/4/16 10:32
     * @author ChenYongJia
     * @version 1.0
     */
    public boolean addOperationLog(SysOperationLog sysOperationLog) {
        log.info("进入 SysOperationLongManager 添加系统操作日志对象方法，参数为：{}", JSONObject.toJSONString(sysOperationLog));
        sysOperationLog.setCreateTime(LocalDateTime.now());
        sysOperationLog.setCreateUserId((long) 1);
        try {
            return iTbSysOperationLogService.save(sysOperationLog);
        } catch (Exception e) {
            log.error("添加系统操作日志对象执行错误参数为======>e：{}", e);
            return false;
        }
    }

    /**
     * 删除本月、本周、今天之前的所有操作记录
     *
     * @param type 删除的日期阶段
     * @return boolean
     * @date 2021-6-21 11:27:08
     * @author ChenYongJia
     * @version 1.0
     */
    public boolean deleteOperationLog(Integer type) {
        log.info("进入 SysOperationLongManager 删除本月、本周、今天之前的所有操作记录方法，参数为：{}", type);
        LambdaQueryWrapper<SysOperationLog> wrapper = new LambdaQueryWrapper<>();
        try {
            switch (type){
                // 删除本月之前
                case 0:
                    wrapper.le(SysOperationLog::getCreateTime, DateReport.getBeginDayOfWeek());
                    break;
                // 删除本周之前
                case 1:
                    wrapper.le(SysOperationLog::getCreateTime, DateReport.getBeginDayOfMonth());
                    break;
                // 删除今天之前
                case 2:
                    wrapper.le(SysOperationLog::getCreateTime, DateReport.getDayBegin());
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return iTbSysOperationLogService.remove(wrapper);
    }

}
